/*
 * Created on 08-mar-2005
 *
 */
package guru.merlin.server;

import guru.merlin.core.Envelope;

import java.util.Enumeration;

import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;

/**
 * @author 
 *
 */
public class ErrorProcessor{
	String                  queueName = null;
	QueueConnectionFactory  queueConnectionFactory = null;
	QueueConnection         queueConnection = null;
	QueueSession            queueSession = null;
	Queue                   queue = null;

	QueueBrowser           queueBrowser = null;

    public static void main(String[] args) {
        if(args.length!=1){
            System.err.println("error: syntax: provide queue name");
            return;
        }
        try{
        new ErrorProcessor(args[0]);
        }
        catch(Throwable t){
            t.printStackTrace();
        }
        
        
    }
    
    ErrorProcessor(String queueName) throws Exception{
        
        System.out.println("Inicio");
		conectionSetup(queueName);
        System.out.println("Conectado a '"+queueName+"'");

        queueBrowser = queueSession.createBrowser(queue);
        System.out.println("Cargado");

        for (Enumeration e = queueBrowser.getEnumeration() ; e.hasMoreElements() ;) {
            Object o=e.nextElement();
    		try{
    	        if (o instanceof ObjectMessage) {
    	            ObjectMessage  msg = (ObjectMessage) o;

    	            Envelope envelope=(Envelope)msg.getObject();
    	            if (envelope==null) {
    	                System.out.println("ES NULO");
    	            }    	            
    	            System.out.println("---------------------------------------------------------------");

    	            System.out.println("Envelope:"+envelope.toString());
    	            

    	            
                    if(envelope.getDestination()!=null){
                        System.out.println("=destination=> "+envelope.getDestination().getClass().getName());                                                        
                    }
                    
                    if(envelope.getOrigin()!=null){
                        System.out.println("=origin=> "+envelope.getOrigin().getClass().getName());                                                        
                    }
                    
    	            System.out.println("OriginRoute:"+envelope.getOriginRoute());
    	            
    	            System.out.println("DestinationRoute:"+envelope.getDestinationRoute());
    	                               
                    if(envelope.getComment()!=null){
                        System.out.println("=comment=> No 'comment' in Envelope");                        
                    }
                    else{
                        System.out.println("=comment=> "+envelope.getComment());                                                
                    }
                    if(envelope.getThrowable()==null){
                        System.out.println("=throwable=> No throwable in Envelope");                        
                    }
                    else{
                        System.out.println("=throwable=>"+envelope.getThrowable().getClass().getName());
                        System.out.println("=throwable=>"+envelope.getThrowable().getLocalizedMessage());
                    }                    
    	            
    	        } else {
    	            throw new InternalError("onMessage(1)@JMSQueue: message was not an instance of ObjectMessage");
    	        }
    		}
    		catch(Throwable t){
    		    System.err.println("CESAR:ERROR:"+t);
    		    //t.printStackTrace();
                

    		}

        }
        

        
        
        System.out.println("Fin");
        
        queueConnection.close();
        
        
    }

   
    
	private void conectionSetup(String qName) throws Exception{
		this.queueName=qName;
        queueConnectionFactory =
            	new com.sun.messaging.QueueConnectionFactory();
                //getQueueConnectionFactory();

            queueConnection = 
                queueConnectionFactory.createQueueConnection();
            queueSession = queueConnection.createQueueSession(false, 
                Session.AUTO_ACKNOWLEDGE);
           queue = new com.sun.messaging.Queue(queueName);
               //getQueue(queueName, queueSession);
	}


}
